﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System.Net.Mail;
using AccesoDatos.Manejador;
using AccesoDatos;


namespace RMA
{
    public partial class frmCorreo : Form
    {
        crCorreoEmbarque rep = new crCorreoEmbarque();
        DataTable Reporte = new DataTable();
        Manejador_Embarque manEm = new Manejador_Embarque();

        string _FolioEmbarque;

        string pdfFile;

        public frmCorreo(string Folio)
        {
            InitializeComponent();
            _FolioEmbarque = Folio;
            pdfFile = "c:\\Embarque " + Folio + ".pdf";
        }

        private void frmCorreo_Load(object sender, EventArgs e)
        {
            this.ControlBox = false;
            txtDe.Text = "informacion@ctin.com.mx";
            try
            {
                Reporte = manEm.GetCorreo(_FolioEmbarque);
                rep.SetDataSource(Reporte);
                crystalReportViewer1.ReportSource = rep;
                crystalReportViewer1.Refresh();
            }
            catch (CrystalReportsException ex)
            {
                MessageBox.Show(ex.ToString());
            }

        }

        private void sendmail(string correo, string asunto, string body)
        {
            try
            {
                string[] Destinatario = correo.Split(';');
                MailMessage Mail = new MailMessage();
                Mail.From = new MailAddress("informacion@ctin.com.mx");
                foreach (string cliente in Destinatario)
                {
                    Mail.To.Add(new MailAddress(cliente));
                }
                Mail.Subject = asunto;
                Mail.Body = body;
                Mail.IsBodyHtml = false;
                Mail.Priority = MailPriority.Normal;
                Attachment myAttachment = new Attachment(pdfFile);
                Mail.Attachments.Add(myAttachment);
                SmtpClient smtp = new SmtpClient();

                smtp.Host = "smtp.ctin.com.mx";
                smtp.Credentials = new System.Net.NetworkCredential("informacion@ctin.com.mx", "informacion");
                smtp.EnableSsl = false;
                smtp.Send(Mail);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void btnEnviar_Click(object sender, EventArgs e)
        {
            try
            {
                ExportOptions CrExportOptions;
                DiskFileDestinationOptions CrDiskFileDestinationOptions = new DiskFileDestinationOptions();
                PdfRtfWordFormatOptions CrFormatTypeOptions = new PdfRtfWordFormatOptions();
                CrDiskFileDestinationOptions.DiskFileName = pdfFile;
                CrExportOptions = rep.ExportOptions;
                CrExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
                CrExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
                CrExportOptions.DestinationOptions = CrDiskFileDestinationOptions;
                CrExportOptions.FormatOptions = CrFormatTypeOptions;
                rep.Export();

                sendmail(txtPara.Text,txtAsunto.Text,txtBody.Text);
                MessageBox.Show("Se envio correo Dept. RMA");
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

    }
}
